﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace Project3
{
    public class Bullet
    {
        public class Shell : Collidable
        {
            public Vector3 position;
            public override Vector3 Position { get { return position; } set { position = value; } }

            public Vector3 orientation;
            public float azimuth;
            public float speed;
            public float life;
        }

        PhibesGame game;
        Model model;
        Vector3 position = Vector3.Zero;
        float rotationaloffset = (float)Math.PI / 2;

        private LinkedList<Shell> shells = new LinkedList<Shell>();

        public LinkedList<Shell> Shells { get { return shells; } }

        public Bullet(PhibesGame game)
        {
            this.game = game;
        }

        public void LoadContent(ContentManager content)
        {
            model = content.Load<Model>("Lego");
        }

        public void Update(GameTime gameTime)
        {
            float delta = (float)gameTime.ElapsedGameTime.TotalSeconds;
            for (LinkedListNode<Shell> node = shells.First; node != null; )
            {
                LinkedListNode<Shell> next = node.Next;
                Shell curr = node.Value;

                Vector3 direction = curr.orientation;
                curr.position += direction * curr.speed * delta;

                curr.life -= delta;
                if (curr.life <= 0)
                {
                    shells.Remove(node);
                }

                node = next;
            }
        }

        public void Draw(GraphicsDeviceManager graphics, GameTime gameTime)
        {
            foreach (Shell shell in shells)
            {
                DrawModel(graphics, model, Matrix.CreateScale(3) * Matrix.CreateRotationY(shell.azimuth + rotationaloffset) * Matrix.CreateTranslation(shell.position));
            }
        }


        private void DrawModel(GraphicsDeviceManager graphics, Model model, Matrix world)
        {
            Matrix[] transforms = new Matrix[model.Bones.Count];
            model.CopyAbsoluteBoneTransformsTo(transforms);

            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.World = transforms[mesh.ParentBone.Index] * world;
                    effect.View = game.Camera.View;
                    effect.Projection = game.Camera.Projection;
                }
                mesh.Draw();
            }
        }
        public void Fire(Vector3 position, float azimuth)
        {
            Shell shell = new Shell();
            shell.position = position;
            shell.azimuth = azimuth;
            shell.orientation = Functions.FromAzimuth(shell.azimuth);
            shell.speed = 2600.0f;
            shell.life = 12.0f;

            shell.AddCollisionPoint(0, 0, 1);

            shells.AddLast(shell);
        }
    }
}
